package com.jiuli.liping.controller;

import com.jiuli.liping.vo.Message;
import com.jiuli.liping.vo.ShoppingCart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.jiuli.liping.service.ShoppingCartService;

import java.util.List;

/**
 * Author zyl
 * Date  2020-03-28
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @RequestMapping(value = {"/list", ""}, method = RequestMethod.GET)
    public Object list() {
        List<ShoppingCart> shoppingCarts = shoppingCartService.findAllList();
        return shoppingCarts;
    }

    @RequestMapping(value = {"/get"}, method = RequestMethod.GET)
    public Object get(@RequestParam String id) {
        ShoppingCart shoppingCart = shoppingCartService.get(id);
        return shoppingCart;
    }

    /**
     * 加入购物车
     * @param shoppingCart
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Message insert(@RequestBody ShoppingCart shoppingCart) {
        Message me = shoppingCartService.insert(shoppingCart);
        return me;
    }

    @RequestMapping(value = "/insertBatch", method = RequestMethod.POST)
    public String insertBatch(@RequestBody List<ShoppingCart> shoppingCarts) {
        if (shoppingCartService.insertBatch(shoppingCarts) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(@RequestBody ShoppingCart shoppingCart) {
        if (shoppingCartService.update(shoppingCart) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(@RequestBody ShoppingCart shoppingCart) {
        if (shoppingCartService.delete(shoppingCart) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    /**
     * 删除所有该用户的所有购物车
     * @param shoppingCart
     * @return
     */
    @RequestMapping(value = "/deleteAll", method = RequestMethod.POST)
    public String deleteAll(@RequestBody ShoppingCart shoppingCart) {
        if (shoppingCartService.deleteAll(shoppingCart) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }



    /**
     * 根据工号拿到所有购物车数据
     * @param
     * @returnl
     */
    @RequestMapping(value = "/findAllCartListByNumber", method = RequestMethod.POST)
    public List<ShoppingCart> findAllCartListByNumber(@RequestBody ShoppingCart shoppingCart) {
        return shoppingCartService.findAllCartListByNumber(shoppingCart);
    }

    /**
     * 购物车商品数量修改
     * @param shoppingCart
     * @return
     */
    @RequestMapping(value = "/updateNumber", method = RequestMethod.POST)
    public String updateNumber(@RequestBody ShoppingCart shoppingCart) {
        int flag =shoppingCartService.updateNumber(shoppingCart);
        if (flag == 1) {
            return "success";
        } else if(flag == 2){
            return "库存不足";
        }
        return "failed";
    }
}
